package com.qk.server.impl;

import com.qk.Vo.OverviewVo;
import com.qk.mapper.BusinessesMapper;
import com.qk.mapper.ClueMapper;
import com.qk.server.ReportServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class ReportServerImpl  implements ReportServer {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessesMapper businessesMapper;
    @Override
    public OverviewVo getOverView() {

        //从Redis中获取总览
        Object overview = redisTemplate.opsForValue().get("overview");
        if(overview != null) {
            log.info("从Redis中获取总览{}", overview);
            return (OverviewVo) overview;
        }
        //获取线索总览
        OverviewVo clueOverviewVo =clueMapper.getClueOverView();
        //获取商机总览
        OverviewVo businessOverviewVo =businessesMapper.getBusinessOverView();
        //封装
        BeanUtils.copyProperties(businessOverviewVo,clueOverviewVo,"clueTotal","clueWaitAllot", "clueWaitFollow", "clueFollowing", "clueFalse", "clueConvertBusiness");
        //保存到Redis中
        redisTemplate.opsForValue().set("overview",clueOverviewVo,30, TimeUnit.SECONDS);
        log.info("从数据库中获取总览{}", clueOverviewVo);
        return clueOverviewVo;
    }
}
